package com.example.theoretical_verification.thread;

// 这个演示的是，没有加任何同步机制，最最原始的线程安全的案例。卖票问题。
public class TicketSellerDemo {
    public static void main(String[] args) {

        // 匿名内部类
        Runnable runnable = new Runnable() {
            private int tickets = 100;

            @Override
            public void run() {
                while (tickets > 0) {
                    sellTicket();
                }
            }

            private void sellTicket() {
                if (tickets > 0) {
                    System.out.println(Thread.currentThread().getName() + " sells ticket " + tickets);
                    tickets--;
                }
            }
        };

        Thread thread1 = new Thread(runnable, "Seller 1");
        Thread thread2 = new Thread(runnable, "Seller 2");
        Thread thread3 = new Thread(runnable, "Seller 3");
        Thread thread4 = new Thread(runnable, "Seller 4");
        Thread thread5 = new Thread(runnable, "Seller 5");
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
    }
}

